<template>
  <div >
     
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
      size="mini"
      type="primary"
            icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['admin:ip_mac:network:post']"
        >新增地址段
        </el-button>
      </el-col>

    </el-row>

        <el-table
    :data="tableData"
    stripe
    style="width: 100%"
    :header-cell-style="{textAlign: 'center'}"
    :cell-style="{ textAlign: 'center' }"
    >
        <el-table-column
      type="index"
      label="序号"
      width="50">
    </el-table-column>
    <el-table-column
      prop="address"
      label="网段信息"
      width="135">
    </el-table-column>
        <el-table-column
      prop="netmask"
      label="子网掩码"
      width="130">
    </el-table-column>
    <el-table-column
      prop="total"
      label="地址总数"
      width="90">
    </el-table-column>
        <el-table-column
      label="空闲总数"
      width="80">
                    <template slot-scope="scope">
                {{  scope.row.total-scope.row.ips}}
              </template>
    </el-table-column>
        <el-table-column
      prop="net"
      label="网络号"
      width="120">
    </el-table-column>
            <el-table-column
      prop="broadcast"
      label="广播地址"
      width="135">
    </el-table-column>
            <el-table-column
      prop="version"
      label="版本"
      width="50">
    </el-table-column>
            <el-table-column
      prop="v46map"
      label="4TO6映射"
      width="170">
    </el-table-column>    
            <el-table-column
      prop="type"
      label="地址类型"
      width="80">
              <template slot-scope="scope">
                {{ network_type[scope.row.type]}}
              </template>
    </el-table-column>  
     <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        v-if="hasPermi(['cmdb:clearsavefile:download:post'])"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="one_edit(scope.row)"
            v-hasPermi="['admin:ip_mac:network:put']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:ip_mac:network:delete']"
          >删除
          </el-button>
        </template>
      </el-table-column>
  </el-table>


    <!-- 对话框 -->
        <el-dialog
      :title="title"
      width="550px"
      :visible.sync="is_visible"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
          <el-form
        :model="dataform"
        label-width="100px"
        :rules="rules"
        label-position="left"
      >
        <el-form-item
          label="IP地址段："
          prop="address"
        >
                <el-input
        type="text"
        placeholder="请输入内容"
        v-model="dataform.address"
        maxlength="50"
        show-word-limit
        >
        </el-input>
        </el-form-item>
        <el-form-item
          label="地址段类型："
        >
  <el-select v-model="network_value" placeholder="请选择">
    <el-option
      v-for="item in network_options"
      :key="item.type"
      :label="item.label"
      :value="item.type">
    </el-option>
  </el-select>
        </el-form-item>

      </el-form>

            <center>
      <el-form label-width="0px" style="margin-right: 30px align:center">
      <el-form-item>
        <el-button size="small" style="width: 20%" type="primary" @click="on_submit()">提交</el-button>
        <el-button size="small" style="width: 20%" type="info"  @click="on_close()" >取消</el-button>
      </el-form-item>
    </el-form>
    </center>
    </el-dialog>
        <el-pagination
      style="float: right; margin-right: 100px"
      background
      layout="total, prev, pager, next"
      :page-size="queryParams.pageSize"
      :total="total"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
    >
    </el-pagination>
  </div>
</template>

<script>
  import {listNetwork, addNetwork, updateNetwork,delNetwork} from "@/api/vadmin/cmdb/ipmac";

  export default {
    name: "network",
    data() {
      return {
               tableData: [{
          ip: '192.168.1.1',
          mac: '00E04C9BCD36',
          prefix: '192.168.1.0/24',
          version:"V4",
          online_time:"2021-07-19 16:29"
        }],
        activeName: 'first',
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        pid:1,
        edit_or_update:0,
        network_options:{},
        network_value:0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          type: undefined,
        },
        // 总条数
        total: 0,
        is_visible:false,
        dataform:{},
                rules:{address:{ required: true, message: '请输入问题描述', trigger: 'blur' },
                     }
      }
    },
    created() {
      this.network_options=this.$store.state.cmdb.network_options
      this.network_type=this.$store.state.cmdb.network_type
      this.getList();
    },
    mounted() {
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        listNetwork(this.queryParams.pageNum,this.queryParams.pageSize).then(response => {
          var options=[]
                                  options=response.results
          options.splice(0,1)
          options.splice(-1,1)
          this.tableData =options;
          this.total = response.total;
          this.loading = false;

        });
      },
      /** 上传按钮操作 */
      handleAdd(row) {
        this.edit_or_update=0
        this.dataform={}
        this.is_visible = true;
        this.title = "新增IP地址段";
      },
      on_close(){
          this.is_visible=false;
      },
      on_submit(){
        // 0编辑，1修改
        if (this.edit_or_update==0){
        addNetwork(this.dataform).then(response=>{
            this.is_visible=false
            this.getList({pid:this.pid})
        })
        }else{
          updateNetwork(this.dataform.id,{"address":this.dataform.address,"type":this.network_value}).then(response=>{
              this.is_visible=false
              this.getList()
          })  
        }

      },
            one_edit(row){
                this.edit_or_update=1
                this.title="编辑状态"
                this.is_visible=true
        this.dataform=row
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除状态为为"' + row.address + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delNetwork(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },

    }

  }
</script>

<style scoped>

</style>
